package com.curso.android.trabajo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class PrincipalActivity extends Activity {

	SharedPreferences mPrefs;
	final String welcomeScreenShownPref = "welcomeScreenShown";
	String versionNumber = "";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_principal);
		
		
		   mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		    // second argument is the default to use if the preference can't be found
		    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

		    if (welcomeScreenShown) {
		    	
		    	 try {
		             versionNumber = getBaseContext().getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
		         } catch (NameNotFoundException e) {
		             e.printStackTrace();
		         }
		        // here you can launch another activity if you like
		        // the code below will display a popup
		        String whatsNewTitle = getResources().getString(R.string.whatsNewTitle) + " " + versionNumber;
		        String whatsNewText = getResources().getString(R.string.whatsNewText);
		        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
		                R.string.ok, new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int which) {
		                        dialog.dismiss();
		                    }
		                }).show();
		        SharedPreferences.Editor editor = mPrefs.edit();
		        editor.putBoolean(welcomeScreenShownPref, false);
		        editor.commit(); // Very important to save the preference
		    }

		final Button bMaps = (Button) findViewById(R.id.main_b_maps);
		bMaps.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Lanzamos Mis Mapas
				Intent intent = new Intent(getApplicationContext(),	MapaLugaresActivity.class);
				startActivity(intent);
			}
		});

		final Button bPlaces = (Button) findViewById(R.id.main_b_places);
		bPlaces.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Lanzamos Mis Sitios
				Intent intent = new Intent(getApplicationContext(),	ListaLugaresActivity.class);
				startActivity(intent);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_principal, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_conocenos:
	        	// Lanzamos Opciones
	        	Intent intent = new Intent(getApplicationContext(),	OpcionesActivity.class);
				startActivity(intent);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

}
